//
//  HomePageVC.swift
//  仿苹果首页动画
//
//  Created by slience on 2019/8/19.
//  Copyright © 2019 shehuiren. All rights reserved.
//

import UIKit

class HomePageVC: UIViewController {
    
    lazy  var titles: [String] = {
        
        return ["冷冷的酷酷的","完美无瑕","不一样的我","看我小翘臀"]
    }()
    
    lazy  var contents: [String] = {
        
        return ["生在起点的女人","秀色可餐的女人","舍我其谁","女王的气质"]
    }()
    
    lazy  var des: [String] = {
        
        return ["山不在高，有仙则名。水不在深，有龙则灵。斯是陋室，惟吾德馨。苔痕上阶绿，草色入帘青。谈笑有鸿儒，往来无白丁。可以调素琴，阅金经。无丝竹之乱耳，无案牍之劳形。南阳诸葛庐，西蜀子云亭。孔子云：何陋之有","轻轻的我走了，正如我轻轻的来；我轻轻的招手，作别西天的云彩。那河畔的金柳，是夕阳中的新娘；波光里的艳影，在我的心头荡漾。软泥上的青荇，油油的在水底招摇；在康河的柔波里，我甘心做一条水草！那榆荫下的一潭，不是清泉，是天上虹；揉碎在浮藻间，沉淀着彩虹似的梦。寻梦？撑一支长篙，向青草更青处漫溯；满载一船星辉，在星辉斑斓里放歌。但我不能放歌，悄悄是别离的笙箫；夏虫也为我沉默，沉默是今晚的康桥！悄悄的我走了，正如我悄悄的来；我挥一挥衣袖，不带走一片云彩","曾经，有一份真诚的爱情放在我面前， 我没有珍惜，等到我失去的时候才后悔莫及，人世间最痛苦的事莫过于此……如果上天能够给我一个再来一次的机会，我会对那个女孩子说三个字：“我爱你。”如果非要在这份爱上加上期限，我希望是……一万年！","至尊宝：观音大士，我开始明白你说的话了，以前我看事物是用肉眼去看。但是在我死去的那一刹那，我开始用心眼去看这个世界，所有的事物真的可以看得前所未有的那么清楚……原来那个女孩子在我的心里面流下了一滴眼泪，我完全可以感受到当时她是多么地伤心……\n 至尊宝：On你妈个头啊！你有完没完啊！(一拳将唐僧打倒)我已经跟你说过我不行了，你还要On-On-！On-On-！完全不理人家受得了受不了，你再On我一刀捅死你！唐僧：悟空，你尽管捅死我吧，生又何哀，死又何苦，等你明白了舍生取义，你自然会回来跟我唱这首歌的！喃呒阿弥陀佛、喃呒阿弥陀佛……\n悟空：大家看到啦？这个家伙没事就长篇大论婆婆妈妈叽叽歪歪，就好象整天有一只苍蝇，嗡……对不起，不是一只，是一堆苍蝇围着你，嗡…嗡…嗡…嗡…飞到你的耳朵里面，救命啊！(悟空倒地翻滚，异常痛苦。)\n我的意中人是个盖世英雄，有一天他会踩着七色的云彩来娶我。我猜中了前头，可是我猜不着这结局......"]
    }()
    
 
    @IBOutlet weak var myTableView : UITableView!
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.isNavigationBarHidden = true
        self.navigationController?.delegate = self

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    

    //MARK: - -截屏
    func imageFromView()-> UIImage{
        
        UIGraphicsBeginImageContext(self.view.frame.size);
        
        let context = UIGraphicsGetCurrentContext()
        
        self.view.layer.render(in: context!)
        
        let theImg = UIGraphicsGetImageFromCurrentImageContext()
        
        return theImg!
    }

}

extension HomePageVC : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "\(HomePageCell.self)", for: indexPath) as! HomePageCell
        
        cell.header.image = UIImage(named: String(format: "image%d",indexPath.row+1))
        cell.titleL.text = titles[indexPath.row]
        cell.contentL.text = contents[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        // 为了让图片显示原来的比例 加20
        return  (UIScreen.main.bounds.size.width - 20*2)/2*3 + 20
    }
    
    
    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        
        let cell = tableView.cellForRow(at: indexPath) as! HomePageCell
        
        cell.startAnimation()
        
        return true
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath) as! HomePageCell

        cell.stopAnimation()

    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "zh") as! ZHViewController
        
        vc.imageName = String(format: "image%d",indexPath.row+1)
        vc.selectIndexPath = indexPath
        vc.titleStr1 = titles[indexPath.row]
        vc.titleStr2 = contents[indexPath.row]
        vc.des = des[indexPath.row]
        vc.bgImage = imageFromView()
        
        self.navigationController?.pushViewController(vc, animated: true)

    }
    
    
   
    
    
}

extension HomePageVC : UINavigationControllerDelegate,UIViewControllerAnimatedTransitioning{
    
    //MARK: - -运用本类的跳转动画
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        
        return self
    }
    //MARK: - - 动画市场
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        
        return 1.0
    }
    
    //MARK: - -动画核心代码
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //去向
        let toVC = transitionContext.viewController(forKey: .to) as! ZHViewController
        
        var cell : HomePageCell?
        if let index = myTableView.indexPathForSelectedRow {
            
            cell = myTableView.cellForRow(at: index) as? HomePageCell
        }
       
        //为动画做准备
        let fromView = cell?.header
        
        let toView = toVC.headImageView
        
        // 容器
        let containerView =  transitionContext.containerView
        
       //最终去向vc view的frame
        toVC.view.frame = transitionContext.finalFrame(for: toVC)
        
        let customImageView = UIImageView(image: cell?.header.image)
        
        if let fromVieww = fromView{
            
             customImageView.frame = containerView.convert(fromVieww.frame, from: fromView?.superview)
        }
        
     //为动画做准备
        let titleLabel = UILabel()
        
        titleLabel.text = toVC.titleStr1
        
        titleLabel.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        
        titleLabel.textColor = .white
       
        titleLabel.frame = CGRect(x: 15, y: 50, width: SCREEN_WIDTH - 30, height: 21)
       
        customImageView.addSubview(titleLabel)
        
        let titleTwoLabel = UILabel()
        
        titleTwoLabel.text = toVC.titleStr2
        
        titleTwoLabel.textColor = .white

        titleTwoLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
       
        titleTwoLabel.frame = CGRect(x: 15, y: SCREEN_WIDTH*1.5 - 15 - 21, width: SCREEN_WIDTH - 30, height: 21)
        
        customImageView.addSubview(titleTwoLabel)

        // 反转过程中展示的
        containerView.addSubview(toVC.view)
        containerView.addSubview(customImageView)
       
        fromView?.isHidden = true
        
        toVC.view.alpha = 0
        
        toView?.isHidden = true
        
        //动画开始
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: .curveEaseIn, animations: {
            
             toVC.view.alpha = 1
            containerView.layoutIfNeeded()

             customImageView.frame = containerView.convert(toView!.frame, from: toView?.superview)
            
            
        }) { (finish) in
            //跳转完 删除 过渡时的控件 展示页面
            fromView?.isHidden = false
            toVC.view.isHidden = false
            toView?.isHidden = false
            customImageView.removeFromSuperview()
            
            // 动画结束 告诉上下文
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
        
    }
    
    
    
}
